﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for mailer
/// </summary>
/// 
namespace greenwerkz.utils
{
    public static class mailer
    {

        public static string EmailToEmailList = "";
        public static string EmailToBCCList = "";
        public static string EmailServer = "127.0.0.1";
        public static string EmailSubject = "";
        public static string EmailMessage = "";
        public static string EmailFromName = "";
        public static string EmailFromEmail = "";
        private static List<string> EmailToEmails
        {
            get
            {
                List<string> lReturn = new List<string>();
                foreach (string sEmail in EmailToEmailList.Split(';'))
                {
                    lReturn.Add(sEmail);
                }
                if (EmailToEmailList.Trim() != "" && EmailToEmailList.Contains("@") && lReturn.Count == 0)
                {
                    lReturn.Add(EmailToEmailList);
                }
                return lReturn;
            }

        }
        private static List<string> EmailBCCEmails
        {
            get
            {
                List<string> lReturn = new List<string>();
                foreach (string sEmail in EmailToBCCList.Split(';'))
                {
                    lReturn.Add(sEmail);
                }
                if (EmailToBCCList.Trim() != "" && EmailToBCCList.Contains("@") && lReturn.Count == 0)
                {
                    lReturn.Add(EmailToBCCList);
                }
                return lReturn;
            }

        }
        public static void sendEmail()
        {
            try
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                //add to address
                foreach (string emailAddy in EmailToEmails)
                {
                    if (emailAddy.Trim() != "")
                    {
                        message.To.Add(emailAddy);
                    }
                }

                //add BCC
                foreach (string emailAddy in EmailBCCEmails)
                {
                    if (emailAddy.Trim() != "")
                    {
                        message.Bcc.Add(emailAddy);
                    }
                }

                //if (BCH.Common.BCHCommon.BCHCopyEmailAddress!="")message.Bcc.Add(BCH.Common.BCHCommon.BCHCopyEmailAddress);

                message.Subject = EmailSubject;
                message.From = new System.Net.Mail.MailAddress(EmailFromEmail, EmailFromName);
                message.Body = EmailMessage;
                //System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("127.0.0.1");
                //no authentication required for hostworks
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.greenwerkz.com");
                //System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("outbound2.e-mmunity.net");
                //System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("webadmin@greenwerkz.com", "web.99!");
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("chad@greenwerkz.com", "climb.99");
                smtp.Credentials = credentials;
                message.IsBodyHtml = true;
                smtp.Send(message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}